package com.gitee.wsl.flow.transform.limiter


import com.gitee.wsl.api.Closeable
import com.gitee.wsl.api.closeOnError
import com.gitee.wsl.coroutines.atomic.CountLimiter
import kotlinx.coroutines.flow.*


suspend inline fun <T:Closeable> Flow<T>.collectLimiting(
    limiter: CountLimiter,
    crossinline action: suspend (value: T) -> Unit
) {
    collect { payload ->
        payload.closeOnError {
            limiter.useRequest()
            action(it)
        }
    }
}

